﻿function Merge-Hash($base,$new){
    if($new -eq $null) {return $base.Clone()}
    if($base -eq $null) {return $new.Clone()}

    foreach($key in $new.keys){
        if($new[$key] -eq $null){
            Write-Host "The key of [$key] is null... skipping"
        }
        elseif ($new[$key] -is [HashTable]) {
            if($base[$key] -eq $null){
                $base[$key]=$new[$key].Clone()
            }
            else {
                if($base[$key] -is [string]){
                    $base[$key]=$new[$key]
                }
                else {
                    $base[$key]=Merge-Hash $base[$key].Clone() $new[$key].Clone()
                }
            }
        }
        elseif ($base[$key] -is [hashtable]) {
            if(($new[$key] -is [String]) -and ([String]::IsNullOrWhiteSpace($new[$key]))){
                # Preserve the base, if the new-key is empty
            }
        }
        else {
            $base[$key]=$new[$key]
        }
    }

    return $base;
}